<template>
  <div class="form_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt srcset />
      </div>
      <el-form class="form_box" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icondenglu" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icondenglu-mima"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <div class="btns">
          <el-button type="primary" round @click="login">登录</el-button>
          <el-button type="info" round @click="resetForm">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      this.$refs.loginFormRef.resetFields()
      this.loginForm.username = this.loginForm.password = ''
    },
    login () {
      this.$refs.loginFormRef.validate(async v => {
        if (!v) return this.$message.error('请输入正确的用户名及密码')
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (!res.meta.status === 200) return this.$message.error('密码错误')
        this.$message.success('登录成功')
        // 将登录成功获取到的token存储到会话存储中
        window.sessionStorage.setItem('token', res.data.token)
        // 通过编程式导航跳转
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style lang="less" >
.form_container {
  height: 100%;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
  .login_box {
    background-color: #fff;
    height: 300px;
    width: 450px;
    border-radius: 45px;
    position: relative;
    .avatar_box {
      position: absolute;
      padding: 5px;
      top: -15%;
      left: 50%;
      transform: translateX(-50%);
      box-sizing: border-box;
      background-color: #ffffff;
      border-radius: 50%;
      border: 1px solid #eeeeee;
      box-shadow: #ffff 0 0 10px;
      img {
        width: 100px;
        height: 100px;
        background-color: #eeeeee;
        border-radius: 50%;
      }
    }
    .form_box {
      width: 100%;
      position: absolute;
      bottom: 30px;
      padding: 0 20px;
      box-sizing: border-box;
      .btns {
        display: flex;
        justify-content:flex-end;
      }
    }
  }
}
</style>
